package baduga.karo;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JLabel;

import baduga.client.Controler;
import baduga.client.Network_Api;
import baduga.client.Player_Constans;
import baduga.core.Core_Gui_ErrorWindow;
import baduga.core.Core_JsonDataSchema;
import baduga.core.Core_Messages;
import baduga.server.Game_Cfg;

//klasa z domyślnym komunikatem wyjścia

/**
 * @author Karolina
 */
public class ExitScreen extends javax.swing.JFrame {

	 
	private static final long serialVersionUID = 1L;
	
	
	private Controler contr;
	
	
	//zmieniamy ikonkę na pasku tytułu
	
    public ExitScreen(Controler newVar) {
    	
    	contr = newVar;
    	
    	setIconImage(Toolkit.getDefaultToolkit().getImage(ExitScreen.class.getResource("/baduga/karo/resources/icon.png")));
        initComponents();
    }

    //zamykanie okna - z komunikatem żądającym potwierdzenia

    private void initComponents() {

        jLabel1 = new JLabel();
        jButtonYes = new JButton();
        jButtonNo = new JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Badugi - Wyjście");
        setResizable(false);
        
        //dodanie akcji dla poszczególnych przycików
        
        jLabel1.setText("Czy na pewno chcesz wyjść?");

        jButtonYes.setText("Tak");
        jButtonYes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonYesMouseClicked(evt);
            }
        });

        jButtonNo.setText("Nie");
        jButtonNo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNoMouseClicked(evt);
            }
        });

        //wygląd okienka
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonYes)
                        .addGap(47, 47, 47)
                        .addComponent(jButtonNo))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonYes)
                    .addComponent(jButtonNo))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }

    	//obsługa poszczególnych przycisków
    
    private void jButtonYesMouseClicked(java.awt.event.MouseEvent evt) {
        
    	if(Network_Api.isConnected())
    	{
    		
    		
    		/*
        	if(!contr.fSendData( dataDisconnect() ))
        	{
        		Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
        	}
        	*/
    		
    		
    		if(!contr.fDisconnect())
    		{
    			Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02FC), Core_Messages.getMessage(0x0100));
    		}
    	}
    	
    	System.exit(0);
    }

    private void jButtonNoMouseClicked(java.awt.event.MouseEvent evt) {
    	
    	this.dispose();
        
    }
    
    
    /**
     * Rozłączenie z Serwerem.
     * Klasa konstruje struktyrę danych którą przesyła do serwera.
     * 
     * @return
     */
    private Core_JsonDataSchema dataDisconnect()
    {
    	Core_JsonDataSchema data = new Core_JsonDataSchema();
    	
    	//Ustawienie kodu
    	data.setKod(0x01);
    	
    	//Ustawienie argumentów
    	data.setArgs
    	(
    			new int[] {
    					0x02
    			}
    	);
    	
    	
    	//Ustawienie danych
    	data.setDataD1
    	(
    		new Object[] {
    			Player_Constans.getPlayerID()
    		}
    	);
    	
    	return data;
    	
    }



   
    private JButton jButtonNo;
    private JButton jButtonYes;
    private JLabel jLabel1;

}
